//==================客户端(C端)======================
const dateKit = require( "./util/dateKit");
const dataKit = require( "./util/dataKit");
const util = require( "./util/util")
const clientService = require( "./util/clientService")

/**
 * 会员身份识别
 */
exports.isTaobaoMember = async (context) => {
    let result = await clientService.isTaobaoMember(context,context.mixNick,context.openId);
    return {code:200,msg:"获取成功",data:result};
};

/**
 * 客户端(用户登录)
 * @param {} context 
 */
exports.login = async (context) => {
  const {openId} = context;
  var {avatar,userNick} = context.data;
  if(!avatar){
        return {code:500,msg:"avatar是必须的"};
  }
  if(!userNick){
        return {code:500,msg:"userNick是必须的"};
  }
  var userInfo = await context.cloud.db.collection('user_info').find({openId:openId},{limit:1});
  userInfo = userInfo.length>0?userInfo[0]:null;
  if(!userInfo){
    userInfo = {
      openId:openId,
      isFirstLogin:true,
      isEyes:false,
      eyesType:null,
      createTime:dateKit.getCurrentDateTime()
    }
  }else{
    userInfo.isFirstLogin = false;
  }
  userInfo.userNick = userNick,
  userInfo.avatar = avatar,
  userInfo.loginTime = dateKit.getCurrentDateTime();
  await dataKit.insertOrUpdate(context,'user_info',userInfo);
  return  {code:200,msg:'登录成功',data:userInfo};
}

/**
 * 获取用户信息
 * @param {*} context 
 */
exports.getUserInfo = async (context) => {
  const {openId} = context
  let userInfo = await clientService.getUserInfoByOpenId(context,openId);
  return {code:200,msg:"操作成功",data:userInfo};
}

/**
 * 修改眼型
 * @param {*} context 
 */
exports.editAReyes = async (context) => {
  const {eyesType} = context.data;
  const {openId} = context;
  let result = await clientService.editAReyes(context,openId,eyesType);
  return result;
}

/**
 * 埋点
 * @param {*} context 
 */
exports.buriedPoint = async (context) => {
    let userData = context.data;
    const {openId} = context;
    userData.userNick = context.userNick;
    let result = await clientService.buriedPoint(context,openId,userData)
    return result;
}

/**
 * 商品加购
 * @param {*} context 
 */
exports.goodsPurchased = async (context) => {
  const {openId,userNick} = context;
  const {productId} = context.data;
  var returt = await clientService.goodsPurchased(context,openId,userNick,productId);
  return returt;
}

